package cn.hx.mall_goods.controller;

import cn.hx.mall_goods.entity.*;
import cn.hx.mall_goods.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PublicController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private ProductCategoryService productCategoryService;
    @Resource
    private BrandService brandService;
    @Resource
    private FlashPromotionService flashPromotionService;

    @Resource
    private SubjectService subjectService;
    @Resource
    private HomeNewProductService homeNewProductService;
    @Resource
    private ProductService productService;
    @Resource
    private SubjectProductRelationService subjectProductRelationService;

    @Resource
    private CommentService commentService;
    @Resource
    private ProductAttributeService productAttributeService;
    @Resource
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;
    @Resource
    private PmsProductAttributeValueService pmsProductAttributeValueService;

    /**
     * 首页
     */
    @GetMapping({"index", "/"})
    public String index(Model model) {
        //秒杀
        model.addAttribute("secitem", flashPromotionService.queryRecentOne());
        List<PromotionProductRelation> relationList = flashPromotionService.getRelationList();
        for (PromotionProductRelation relation : relationList) {
            relation.setProduct(productService.getOne(new QueryWrapper<Product>().eq("id", relation.getProductId())));
            relation.setFlashPromotion(flashPromotionService.queryOne(relation.getFlashPromotionId()));
        }
        model.addAttribute("secList", relationList);

        //专题
        List<Product> products = productService.list(null);
        List<Subject> recommendSubjects = subjectService.list(new QueryWrapper<Subject>().eq("recommend_status", 1));
        List<Subject> subjects = recommendSubjects;
        if (recommendSubjects.size() > 3) {
            subjects = recommendSubjects.subList(0, 3);
        }

        Map<Long, List<Product>> map = new HashMap<>();
        for (Subject subject : subjects) {
            List<SubjectProductRelation> subjectProductRelationList = subjectProductRelationService.list(new QueryWrapper<SubjectProductRelation>().eq("subject_id", subject.getId()));
            List<Product> list = new ArrayList<>();
            for (SubjectProductRelation subjectProductRelation : subjectProductRelationList) {
                for (Product product : products) {
                    if (product.getId().equals(subjectProductRelation.getProductId())) {
                        list.add(product);
                    }
                }
            }
            if (list.size() > 2) {
                list = list.subList(0, 2);
            }
            map.put(subject.getId(), list);
        }

        //分类集合
        model.addAttribute("categoryList", productCategoryService.list());

        //品牌集合
        model.addAttribute("brandList", brandService.list());

        //新品推荐
        model.addAttribute("newRecommendProducts", homeNewProductService.list(new QueryWrapper<HomeNewProduct>().eq("recommend_status", 1)));

        //所有产品集合
        model.addAttribute("products", products);

        //专题推荐表
        model.addAttribute("recommendSubjects", subjects);

        //推荐专题下商品集合
        model.addAttribute("allProducts", map);

        model.addAttribute("product", products.subList(0, 8));

        return "index";
    }

    /**
     * 商品列表
     */
    @GetMapping("productList")
    public String goodsList(Model model, String categoryName) {
        model.addAttribute("categoryName", categoryName);
        model.addAttribute("products", productService.list(new QueryWrapper<Product>().eq("product_category_name", categoryName)));
        return "productList";
    }

    /**
     * 前端页面测试
     */
    @GetMapping("{path}")
    public String togo(@PathVariable("path") String path) {
        return path;
    }


    @GetMapping("secTime")
    @ResponseBody
    public List<PromotionSession> getSecTime() {
        return flashPromotionService.getSecTime();
    }

    @GetMapping("ywt-product")
    public String getPage(Model model, Integer id) {
        List<PmsProductAttributeValue> pmsProductAttributeValues =
                pmsProductAttributeValueService.list(new QueryWrapper<PmsProductAttributeValue>().eq("product_id", id));
        Product product = productService.getById(id);
        List<Comment> comments = commentService.list(new QueryWrapper<Comment>().eq("product_id", id));
        model.addAttribute("product", product);
        model.addAttribute("comments", comments);
        model.addAttribute("pmsProductAttributeValues", pmsProductAttributeValues);
        return "ywt-product";
    }

    @GetMapping("secList")
    public String secList(Model model) {
        List<PromotionProductRelation> relationList = flashPromotionService.getRelationList();
        for (PromotionProductRelation relation : relationList) {
            relation.setProduct(productService.getOne(new QueryWrapper<Product>().eq("id", relation.getProductId())));
            relation.setFlashPromotion(flashPromotionService.queryOne(relation.getFlashPromotionId()));
//            System.out.println(relation);
        }
        model.addAttribute("secList", relationList);
        return "killsPage";
    }

}
